﻿using System;
using System.Windows;
using UnitFundProfitability.BusinessLogic;

namespace UnitFundProfitability
{
    /// <summary>
    /// Interaction logic for Prices.xaml
    /// </summary>
    public partial class Prices : Window
    {
        #region Constructors
        public Prices()
        {
            InitializeComponent();
            ExistingUnitFunds.DataContext = Manager.Instance.GetUnitFunds();
            PriceDate.SelectedDate = DateTime.Today.AddDays(-1);
            AddPrice.Click += new RoutedEventHandler(AddPrice_Click);
        }
        #endregion

        #region Methods
        private void AddPrice_Click(object sender, RoutedEventArgs e)
        {
            UnitFund selectedUnitFund = ExistingUnitFunds.SelectedValue as UnitFund;
            if (selectedUnitFund == null)
            {
                MessageBox.Show("Please, select unit fund.", "Select unit fund");
            }
            else
            {
                decimal priceValue;
                if (!decimal.TryParse(Price.Text, out priceValue))
                {
                    MessageBox.Show("Please, enter a valid number.", "Invalid number");
                }
                else
                {
                    if (!PriceDate.SelectedDate.HasValue)
                    {
                        MessageBox.Show("Please, select date.", "Select date");
                    }
                    else
                    {
                        UnitPrice price = new UnitPrice();
                        price.PriceDate = PriceDate.SelectedDate.Value;
                        price.FundId = selectedUnitFund.FundId;
                        price.UnitPriceId = Guid.NewGuid();
                        price.Price = priceValue;
                        Manager.Instance.SetUnitPrice(price);
                    }
                }
            }
        }
        #endregion
    }
}